package wrapper;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.StringTokenizer;

import vlms.bean.Cart;
import vlms.bean.Movie;

public class CartMovie {
	private int movieid;
	private String moviename;
	private String moviebanner;
	private Date releasedate;
	private float rentamount;
	private String category;
	private int copiesavailable;

	private int cartid;
	private int personid;
	
	public int getMovieid() {
		return movieid;
	}
	public void setMovieid(int movieid) {
		this.movieid = movieid;
	}
	public String getMoviename() {
		return moviename;
	}
	public void setMoviename(String moviename) {
		this.moviename = moviename;
	}
	public String getMoviebanner() {
		return moviebanner;
	}
	public void setMoviebanner(String moviebanner) {
		this.moviebanner = moviebanner;
	}
	public Date getReleasedate() {
		return releasedate;
	}
	public void setReleasedate(Date releasedate) {
		this.releasedate = releasedate;
	}
	public float getRentamount() {
		return rentamount;
	}
	public void setRentamount(float rentamount) {
		this.rentamount = rentamount;
	}
	public String getCategory() {
		return category;
	}
	public void setCategory(String category) {
		this.category = category;
	}
	public int getCopiesavailable() {
		return copiesavailable;
	}
	public void setCopiesavailable(int copiesavailable) {
		this.copiesavailable = copiesavailable;
	}
	public int getIssueid() {
		return cartid;
	}
	public void setIssueid(int issueid) {
		this.cartid = issueid;
	}
	public int getPersonid() {
		return personid;
	}
	public void setPersonid(int personid) {
		this.personid = personid;
	}
	
	public static CartMovie convertToCartMovie(String input) throws Exception
	{
		CartMovie cm = new CartMovie();
		
		StringTokenizer str = new StringTokenizer(input, ":#");
		
		if (str.countTokens() != 9)
		{
			cm.movieid = Integer.parseInt(str.nextToken());
			cm.moviename = str.nextToken();
			cm.moviebanner = str.nextToken();
			cm.releasedate  = new SimpleDateFormat("yyyy-mm-dd").parse(str.
								  nextToken());
			cm.rentamount = Float.parseFloat(str.nextToken());
			cm.category = str.nextToken();;
			cm.copiesavailable = Integer.parseInt(str.nextToken());

			cm.cartid = Integer.parseInt(str.nextToken());
			cm.personid = Integer.parseInt(str.nextToken());
		}
		else
		{
			throw new Exception("Invalid string provided for populating the " +
								"wrapper.");
		}
		return cm;
	}
	
	public static CartMovie mixObjects(Movie movie, Cart cart)
	{
		CartMovie cm  = new CartMovie();
		cm.movieid = movie.getMovieid();
		cm.moviename = movie.getMoviename();
		cm.moviebanner = movie.getMoviebanner();
		cm.releasedate  = movie.getReleasedate();
		cm.rentamount = movie.getRentamount();
		cm.category = movie.getCategory();
		cm.copiesavailable = movie.getCopiesavailable();

		cm.cartid = cart.getCartid();
		cm.personid = cart.getPersonid();
		return cm;
	}
	public static CartMovie[] mixObjects(Movie[] movie,Cart[] cart) 
	{
		CartMovie cm[]; 
		int length = movie.length;
		
		cm = new CartMovie[length];
		
		for (int index = 0; index < length; index++)
		{
			cm[index] = new CartMovie();
			cm[index].movieid = movie[index].getMovieid();
			cm[index].moviename = movie[index].getMoviename();
			cm[index].moviebanner = movie[index].getMoviebanner();
			cm[index].releasedate  = movie[index].getReleasedate();
			cm[index].rentamount = movie[index].getRentamount();
			cm[index].category = movie[index].getCategory();
			cm[index].copiesavailable = movie[index].getCopiesavailable();

			cm[index].cartid = cart[index].getCartid();
			cm[index].personid = cart[index].getPersonid();
		}
		return cm;
	}
	public static String convertToString(CartMovie cart)
	{
		String result = new String();
		
		result = cart.movieid+":#" + cart.moviename+":#" + cart.moviebanner+":#" + new SimpleDateFormat("yyyy-mm-dd").format(cart.releasedate)+":#" + cart.rentamount
		+":#" + cart.category +":#" + cart.copiesavailable+":#" + cart.cartid+":#" + cart.personid;
		return result;
	}
	public static CartMovie[] convertStringToCartMovieArray(String input[]) throws Exception
	{
		CartMovie psc[];
		int inputLen = input.length;
		
		psc = new CartMovie[inputLen];
		
		for (int index = 0; index < inputLen; index++)
		{
			psc[index] = convertToCartMovie(input[index]);
		}
		return psc;
	}
	public static String[] convertCartMovieToStringArr(CartMovie input[])
	{
		String result[];
		int inputLen = input.length;
		
		result = new String[inputLen];
		
		for (int index = 0; index < inputLen; index++)
		{
			result[index] = convertToString(input[index]);
		}
		return result;
	}
}
